<?php
/**
 * $Id: TpDiagnostics.php 1991 2009-07-23 14:12:50Z rdg $
 * 
 * LICENSE INFORMATION
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details:
 * 
 * http://www.gnu.org/copyleft/gpl.html
 * 
 * 
 * @author Renato De Giovanni <renato [at] cria . org . br>
 * @author Dave Vieglais (Biodiversity Research Center, University of Kansas)
 * 
 */

require_once(dirname(__FILE__).'/../www/tapir_globals.php');
// tapir_diagnostics.php is generated by applying diag_codes_php.xslt 
// against diag_codes.xml
require_once(dirname(__FILE__).'/../www/tapir_diagnostics.php');
require_once(dirname(__FILE__).'/TpDiagnostic.php');
require_once(dirname(__FILE__).'/TpUtils.php');

/**
* Class TpDiagnostics
*
*/
class TpDiagnostics
{
    /**
     * Returns the current diagnostics array
     */
    function &_GetStack( )
    {
        static $stack;

        if ( ! isset( $stack ) ) 
        {
            $stack = array();
        }

        return $stack;

    } // end of member function _GetStack

    /**
     * Adds a diagnostic to the end of the session's list of diagnostics
     * @param cd = the diagnostic code
     * @param msg = the message associated with the diagnostic
     * @param severity = severity level for the diagnostic
     */
    function Append( $cd, $msg, $severity )
    {
        if ( $severity == DIAG_WARN and TP_DIAG_LEVEL > 2 )
        {
            return;
        }

        if ( $severity == DIAG_INFO and TP_DIAG_LEVEL > 1 )
        {
            return;
        }

        if ( $severity == DIAG_DEBUG and TP_DIAG_LEVEL > 0 )
        {
            return;
        }

	global $g_dlog;

        //if ( is_object( $g_dlog ) )
        //{
        //    $g_dlog->debug("Appending ".$severity." when level is ".TP_DIAG_LEVEL);
        //}

        $stack =& TpDiagnostics::_GetStack();

        array_push( $stack, new TpDiagnostic( $cd, $msg, $severity ) );

        if ( is_object( $g_dlog ) )
        {
            $g_dlog->debug( '>> System message: '.$msg );
        }

    } // end of Append

    /**
     * Returns the number of diagnostics in the list
     * @param severity = array of error levels.
     */
    function Count( $severity=array( DIAG_ERROR, DIAG_FATAL ) )
    {
        $stack =& TpDiagnostics::_GetStack();

        $ntot = 0;

        for( $i = 0; $i < sizeof( $stack ); $i++ ) 
        {
            if ( in_array( $stack[$i]->GetSeverity(), $severity ) ) 
            {
                $ntot++;
            }
        }

        return $ntot;

    } // end of Count

    /**
     * Prints out the errors
     */
    function Dump()
    {
        $stack =& TpDiagnostics::_GetStack();

        $n = count( $stack );

        $str = "Diagnostics [$n]: ";

        for ( $i = 0; $i < $n; $i++ )
        {
            $str .= "\n".$stack[$i]->ToString();
        }

        return $str;

    } // end of Dump

    /**
     * Returns an XML representation of the current diagnostics.
     */
    function GetXml()
    {
        $stack =& TpDiagnostics::_GetStack();

        if ( _DEBUG )
        {
            $duration = sprintf( '%0.4f', TpUtils::MicrotimeFloat()-INITIAL_TIMESTAMP );
            TpDiagnostics::Append( DC_DURATION, $duration, DIAG_INFO );

            if ( function_exists( 'memory_get_peak_usage' ) )
            {
                $bytes = sprintf( '%0.1f MB', memory_get_peak_usage()/1024/1024 );
                TpDiagnostics::Append( DC_MEM_PEAK, $bytes, DIAG_INFO );
            }
        }

        $s = '';

        if ( count( $stack ) )
        {
            for ( $i = 0; $i < count( $stack ); $i++ )
            {
                $diag = $stack[$i];
                if ( $diag->GetSeverity() != DIAG_DEBUG or _DEBUG )
                {
                    $s .= $diag->GetXml();
                }
            }

            if ( strlen( $s ) )
            {
                $s = "\n<diagnostics>$s\n</diagnostics>";
            }
        }

        return $s;

    } // end of GetXml

    /**
     * Returns an aray of error messages
     */
    function GetMessages()
    {
        $stack =& TpDiagnostics::_GetStack();

        $n = count( $stack );

        $ret = array();

        for ( $i = 0; $i < $n; $i++ )
        {
            array_push( $ret, $stack[$i]->GetDescription() );
        }

        return array_unique( $ret );

    } // end of GetMessages

    /**
     * Returns the last error, or NULL if none.
     */
    function PopDiagnostic()
    {
        $stack =& TpDiagnostics::_GetStack();

        return array_pop( $stack );

    } // end of PopDiagnostic

    /**
     * Resets the error stack.
     */
    function Reset()
    {
        $stack =& TpDiagnostics::_GetStack();

        $stack = array();

    } // end of Reset

} // end of TpDiagnostics
?>
